package com.bw.zg3_day05zy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.entity.NoticeBeanItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3_day05zy.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/MainActivity")
class MainActivity : BaseActivity<ActivityMainBinding, NoticeViewModel>() {
    val noticeAdapter by lazy { NoticeAdapter() }
    override fun initView() {
        binding.rv.apply {
            adapter = noticeAdapter
            layoutManager = LinearLayoutManager(this@MainActivity)
        }
        noticeAdapter.setOnItemClickListener(object :NoticeAdapter.OnItemClickListener{
            override fun onItemClick(item: NoticeBeanItem, position: Int) {
                Log.i("zzz", "${item}${position}")
                ARouter.getInstance().build("/app/NewsActivity").navigation()
            }
        })
        noticeAdapter.setOnItemLongClickListener(object :NoticeAdapter.OnItemLongClickListener{
            override fun onItemLongClick(item: NoticeBeanItem): Boolean {
                Log.i("zzz", "${item}")
                return true
            }
        })

        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val layoutPosition = viewHolder.layoutPosition
                noticeAdapter.notifyItemRemoved(layoutPosition)
            }
        }).attachToRecyclerView(binding.rv)
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.getNotice().collect{
                noticeAdapter.submitData(it)
            }
        }
    }
}